<script setup>
import Guide from '@/components/Guide.vue'
import { alipayPay } from '@/apis/payFees.js'
import { showToast } from 'vant';
import { useRouter } from 'vue-router'
import { ref } from "vue";


const router = useRouter()

const form = ref({
    payAmount: '',
    studentName: '',
    telephone: '',
    idcard: ''
})
const rules = {
    payAmount: [
        { required: true, message: '请输入缴费金额', trigger: 'blur' },
        { pattern: /^\d+(\.\d{1,2})?$/, message: '请输入有效的金额', trigger: 'blur' }
    ],
    studentName: [
        { required: true, message: '请输入用户名', trigger: 'blur' },
        { min: 2, max: 5, message: '长度在 2 到 5 个字符', trigger: 'blur' }
    ],
    telephone: [
        { required: true, message: '请输入手机号', trigger: 'blur' },
        { pattern: /^1[3-9]\d{9}$/, message: '请输入有效的手机号', trigger: 'blur' }
    ],
    idcard: [
        { required: true, message: '请输入身份证号', trigger: 'blur' },
        { pattern: /^[1-9]\d{5}(18|19|20)\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}(\d|X)$/, message: '请输入有效的身份证号', trigger: 'blur' }
    ],
}
const selectList = ref([
    {
        goodsType: 1,
        goodsName: '专升本'
    },
    {
        goodsType: 2,
        goodsName: '成教'
    }
])
const selectShow = ref(false)

const activeSelect = ref(selectList.value[0])


const validateForm = () => {
    for (const key in rules) {
        for (const rule of rules[key]) {
            if (rule.required && !form.value[key]) {
                showToast(rule.message);
                return false;
            }
            if (rule.pattern && !rule.pattern.test(form.value[key])) {
                showToast(rule.message);
                return false;
            }
            if (rule.min && form.value[key].length < rule.min) {
                showToast(rule.message);
                return false;
            }
            if (rule.max && form.value[key].length > rule.max) {
                showToast(rule.message);
                return false;
            }
        }
    }
    return true;
};


async function alipayPayFn() {
    let data = {
        ...form.value,
        ...activeSelect.value
    }
    const res = await alipayPay(data)
    if (res.data) {
        const container = document.createElement('div');
        container.innerHTML = res.data
        document.body.appendChild(container);
        document.forms[0].submit();
        container.remove();
    } else {
        showToast('支付链接获取失败');
    }

}

function clickSelectItem(e) {
    activeSelect.value = e
    selectShow.value = false
}

function payFn() {
    if (!validateForm()) {
        return
    }
    alipayPayFn()
    // router.push({ name: 'payFeesDetails' })

}
</script>

<template>
    <div class='payFees_page'>
        <NavBar></NavBar>
        <div class="container">
            <div class="card_box">
                <div class="top_title">
                    <div class="item" :class="{ active: item.goodsType === activeSelect.goodsType }"
                        v-for="item of selectList" @click="clickSelectItem(item)">{{ item.goodsName }}</div>
                </div>
                <div class="money_box">
                    <div class="title">缴费金额</div>
                    <div class="num">
                        <div class="leftIcon">￥</div>
                        <div class="money">
                            <input type="number" v-model="form.payAmount" class="moneyIpt" placeholder="请输入缴费金额">
                        </div>
                    </div>
                </div>
                <div class="form_box">
                    <div class="form_item">
                        <div class="label">姓名</div>
                        <input type="text" v-model="form.studentName" class="valueIpt" placeholder="请输入学生姓名">
                    </div>
                    <div class="form_item">
                        <div class="label">手机号</div>
                        <input type="text" v-model="form.telephone" class="valueIpt" placeholder="请输入学生手机号">
                    </div>
                    <div class="form_item">
                        <div class="label">身份证</div>
                        <input type="text" v-model="form.idcard" class="valueIpt" placeholder="请输入学生身份证">
                    </div>
                </div>
            </div>
        </div>
        <div class="payBtn" @click="payFn">立即支付</div>
    </div>
    <Guide></Guide>
</template>

<style lang='scss' scoped>
.payFees_page {
    height: 100vh;
    background: linear-gradient(180deg, #E82925 0%, #E82925 18%, #F5F5F5 38%);
    overflow: hidden;

    .container {
        padding: 0 15px;

        .card_box {
            padding: 15px 15px 15px;
            border-radius: 12px;
            background-color: #fff;

            .top_title {
                display: flex;
                align-items: center;
                justify-content: space-between;
                gap: 13px;

                .item {
                    box-sizing: border-box;
                    flex: 1;
                    height: 41px;
                    line-height: 41px;
                    background: #F8F8F8;
                    border-radius: 8px;
                    text-align: center;
                    font-size: 15px;
                    color: #121212;
                    border: 2px solid transparent;


                    &.active {
                        background: rgba(215, 36, 32, 0.1);
                        border: 2px solid #D72420;
                        color: #D72420;
                        font-weight: bold;
                    }
                }
            }

            .money_box {
                margin-top: 20px;
                border-bottom: 1px solid #E7E7E7;
                padding-bottom: 13px;

                ov .title {
                    font-size: 15px;
                    color: #323232;
                }

                .num {
                    display: flex;
                    align-items: center;
                    gap: 8px;
                    margin-top: 23px;

                    .leftIcon {
                        font-size: 24px;
                        color: #323232;
                    }

                    .money {
                        flex: 1;
                        overflow: hidden;

                        .moneyIpt {
                            width: 100%;
                            font-size: 38px;
                            border: none;

                            &::placeholder {
                                // font-size: 15px;
                                font-size: 20px;
                                color: #AEAEAE;

                            }
                        }
                    }
                }
            }

            .form_box {
                margin-top: 20px;

                .form_item {
                    display: flex;
                    align-items: center;
                    gap: 20px;
                    margin-bottom: 30px;

                    &:last-child {
                        margin-bottom: 0;
                    }


                    .label {
                        font-size: 15px;
                        color: #323232;
                    }

                    .valueIpt {
                        flex: 1;
                        border: none;
                        text-align: right;
                        background-color: transparent;

                        &::placeholder {
                            font-size: 14px;
                            color: #B0B0B0;
                            text-align: right;
                        }
                    }
                }
            }
        }

    }

    .payBtn {
        width: 315px;
        height: 48px;
        line-height: 48px;
        background: #D72420;
        border-radius: 25px;
        text-align: center;
        color: #FFF;
        margin: 45px auto 0;
    }
}
</style>